#!/bin/bash

format_file() {
  file="${1}"

  [ ${file#src/} == ${file} ] && return  # don't startswith src/
  [ ${file#src/ext} != ${file} ] && return  # startswith src/ext

  if [ -f $file ]; then
    clang-format -style file -i ${1}
    git add ${1}
  fi
}

case "${1}" in
  --about )
    echo "Runs clang-format on source files"
    ;;
  * )
    for file in `git diff-index --cached --name-only HEAD | grep -iE '\.(cpp|cc|h|hpp)$' ` ; do
      format_file "${file}"
    done
    ;;
esac
